package cn.com.gensoft.app.core.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.Map;

/**
 * 密码加密
 * @author hlj
 */
public class PasswordUtil {

    private static RandomNumberGenerator randomNumberGenerator = null;
    //md5算法
    private static final String algorithmName = "md5";
    //迭代3次
    private static final int hashIterations = 3;

    public static Map<String, String> encryptPassword(String username, String password) {
        randomNumberGenerator = new SecureRandomNumberGenerator();
        Map<String, String> map = new HashMap<String, String>();
        String salt = randomNumberGenerator.nextBytes().toHex();
        map.put("salt", salt);
        String newPass = new SimpleHash(algorithmName, password,
            ByteSource.Util.bytes(username + salt), hashIterations).toHex();
        map.put("password", newPass);
        return map;
    }
}
